﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jemgine.Hex
{
    public enum Direction
    {
        First = 0,

        HorizontalNorth = 0,
        HorizontalNorthEast = 1,
        HorizontalSouthEast = 2,
        HorizontalSouth = 3,
        HorizontalSouthWest = 4,
        HorizontalNorthWest = 5,

        VerticalNorthEast = 0,
        VerticalEast = 1,
        VerticalSouthEast = 2,
        VerticalSouthWest = 3,
        VerticalWest = 4,
        VerticalNorthWest = 5,

        Last = 5,

        NumberOfDirections = 6,
    }

    public static class Directions
    {
        public static Direction Rotate(Direction Dir, int Amount)
        {
            if (Dir < Direction.First || Dir > Direction.Last || System.Math.Abs(Amount) > (int)Direction.NumberOfDirections)
                throw new InvalidOperationException("Directions out of range.");

            Dir += Amount;
                        
            int n_dir = (int)Dir % (int)Direction.NumberOfDirections;
            if (n_dir < 0) n_dir = (int)Direction.NumberOfDirections + n_dir;
            Dir = (Direction)n_dir;

            return Dir;
        }

        public static Direction Opposite(Direction Dir) { return Rotate(Dir, 3); }

        public static int Rotate(int Dir, int Amount)
        {
            Dir += Amount;
            Dir = Dir % 6;
            if (Dir < 0) Dir = 6 + Dir;
            return Dir;
        }

        public static int Opposite(int Dir, int Amount) { return Rotate(Dir, 3); }
    }

}